﻿using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace CrmRulo
{
    class ConfigurationFileWriter
    {
        private readonly string _inputFolder;

        public ConfigurationFileWriter(string inputFolder)
        {
            _inputFolder = inputFolder;
        }

        /// <summary>
        /// Creates a boilerplate configuration file for the reports found in the specified directory.
        /// </summary>
        public void CreateConfigurationFile()
        {
            var config = new XElement("publish", new XAttribute("directory", _inputFolder));
            var reportFiles = Directory.GetFiles(_inputFolder).Where(f => f.EndsWith(".rdl"));

            foreach (var file in reportFiles)
            {
                var fileName = new FileInfo(file).Name;
                config.Add(new XElement("report",
                    new XAttribute("filename", fileName),
                    new XAttribute("name", fileName),
                    new XAttribute("description", fileName),
                    new XAttribute("parent", "PARENTREPORT"),
                    new XAttribute("languagecode", "-1"),

                        new XElement("entity", "ENTITYLOGICALNAME_1"),
                        new XElement("entity", "ENTITYLOGICALNAME_2"),

                        new XElement("visibility", ReportVisibilityCode.Form),
                        new XElement("visibility", ReportVisibilityCode.Grid),
                        new XElement("visibility", ReportVisibilityCode.ReportsGrid),

                        new XElement("category", ReportCategoryCode.SalesReports),
                        new XElement("category", ReportCategoryCode.ServiceReports),
                        new XElement("category", ReportCategoryCode.MarketingReports),
                        new XElement("category", ReportCategoryCode.AdministrativeReports)
                    ));
            }
            config.Save(_inputFolder + "\\publishconfig.xml");
        }
    }
}